package com.example.qixinshangwutong.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qixinshangwutong.R;

import org.jetbrains.annotations.NotNull;

import cn.leancloud.LCUser;
import cn.leancloud.types.LCNull;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity {


    public static final String TAG = LoginActivity.class.getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();

    }


    public void initView(){

        EditText loginUserNameEdit = findViewById(R.id.login_username_text);

        EditText loginPhoneCode = findViewById(R.id.login_phone_code_text);

        Button btnLoginGetCode = findViewById(R.id.login_get_phone_code_btn);

        Button btnLogin = findViewById(R.id.login_btn);

        TextView btnToRegister = findViewById(R.id.login_to_register_btn);

        TextView btnLoginByPwd = findViewById(R.id.login_by_pwd_btn);

        ImageButton imageButton = findViewById(R.id.login_go_back_btn);

        btnLoginGetCode.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                String phoneNum = loginUserNameEdit.getText().toString();

                LCUser.requestLoginSmsCodeInBackground(phoneNum)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<LCNull>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull LCNull lcNull) {
                                Toast.makeText(LoginActivity.this, "短信发送成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(@NotNull Throwable e) {
                                Toast.makeText(LoginActivity.this, "短信发送失败", Toast.LENGTH_SHORT).show();
                                Log.d(TAG, "error" + e.getMessage(), e);
                            }

                            @Override
                            public void onComplete() {

                            }
                        });




            }
        });

        btnLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                String phoneCode = loginPhoneCode.getText().toString();

                String phoneNum = loginUserNameEdit.getText().toString();

                LCUser.signUpOrLoginByMobilePhoneInBackground(phoneNum,phoneCode)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<LCUser>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull LCUser lcUser) {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                /*Intent intent = new Intent(LoginActivity.this,BottomActivity.class);
                                startActivity(intent);*/

                                LoginActivity.this.finish();
                            }

                            @Override
                            public void onError(@NotNull Throwable e) {
                                Toast.makeText(LoginActivity.this, "验证码不正确", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });

        btnToRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);

                startActivity(intent);

            }
        });


        btnLoginByPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,PwdLoginActivity.class);

                startActivity(intent);
            }
        });

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,BottomActivity.class);

                startActivity(intent);
            }
        });

    }

}